﻿using System.Configuration;
using System.IO;
using System.Reflection;
using DimensionsHelper.Client.Properties;
using DimensionsHelper.Common.Utils;

namespace DimensionsHelper.Client.ViewModels;

public class JsonOptionalFileSource
{
    private static readonly Assembly Assembly = typeof(JsonOptionalFileSource).Assembly;
    private static readonly ApplicationSettingsBase Settings = DefaultSettings.Default;


    private readonly HashSet<string> _presetResourceName;
    private StringCollection? _cachedCollection;
    private string _historySettingName = string.Empty;

    public JsonOptionalFileSource(JsonLoaderViewModelBase viewModel)
    {
        _presetResourceName = [.. PresetJsonSource.Instance.GetPresetResourceNames(viewModel.Section)];
        OptionalJsonList = [.. _presetResourceName];
        SetHistorySettingName(viewModel.HistoryJsonSettingName);
    }

    public ObservableCollection<string> OptionalJsonList { get; }


    public bool IsPresetJson(string jsonName)
    {
        return _presetResourceName.Contains(jsonName);
    }


    public bool IsPresetOrHistoryJson(string jsonName)
    {
        return OptionalJsonList.Contains(jsonName);
    }


    public void ClearHistoryJson()
    {
        OptionalJsonList.Clear();
        OptionalJsonList.AddRange(_presetResourceName);
    }


    public string ReadJson(string jsonName)
    {
        // 预设值
        if (IsPresetJson(jsonName))
        {
            using Stream? resource =
                Assembly.GetManifestResourceStream($"DimensionsHelper.Client.Resources.{jsonName}");

            if (resource == null)
            {
                return string.Empty;
            }

            using StreamReader reader = new(resource);
            return reader.ReadToEnd();
        }

        if (!File.Exists(jsonName))
        {
            return string.Empty;
        }

        SetHistoryJsonName(jsonName);
        return File.ReadAllText(jsonName);
    }


    private void SetHistorySettingName(string historySettingName)
    {
        _historySettingName = historySettingName;

        if (Settings[_historySettingName] is not StringCollection list)
        {
            list = new StringCollection();
            _cachedCollection = list;
            Settings[_historySettingName] = list;
            SaveHistorySettingValue();
        }
        else
        {
            _cachedCollection = list;
        }

        foreach (string? item in list)
        {
            if (string.IsNullOrEmpty(item))
            {
                continue;
            }

            OptionalJsonList.Add(item);
        }
    }


    private void SetHistoryJsonName(string historyJsonName)
    {
        StringCollection collection = GetHistorySettingValue();
        if (collection.Contains(historyJsonName))
        {
            return;
        }

        collection.Add(historyJsonName);
        OptionalJsonList.Add(historyJsonName);
        SaveHistorySettingValue();
    }


    public void RemoveHistoryJsonName(string historyJsonName)
    {
        StringCollection collection = GetHistorySettingValue();
        collection.Remove(historyJsonName);
        OptionalJsonList.Remove(historyJsonName);
        SaveHistorySettingValue();
    }


    private StringCollection GetHistorySettingValue()
    {
        if (_cachedCollection == null)
        {
            throw new Exception("'HistorySettingName' is not set.");
        }

        return _cachedCollection;
    }


    private void SaveHistorySettingValue()
    {
        if (_cachedCollection == null || string.IsNullOrEmpty(_historySettingName))
        {
            throw new Exception("'HistorySettingName' is not set.");
        }

        Settings[_historySettingName] = _cachedCollection;
        Settings.Save();
    }
}